package com.silence.gankio.activity.web;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.view.KeyEvent;
import com.silence.gankio.R;
import com.silence.gankio.mvvm.BaseActivity;
import com.silence.gankio.mvvm.viewModel.BaseViewModel;
import com.xuexiang.xui.utils.SnackbarUtils;

import static com.silence.gankio.activity.web.AgentWebFragment.KEY_URL;


public class AgentWebActivity extends BaseActivity<BaseViewModel> {

    @org.jetbrains.annotations.Nullable
    @Override
    public Class<BaseViewModel> getModelClass() {
        return null;
    }

    @Override
    public int getActivityContent() {
        return R.layout.activity_agent_web;
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        hideToolBar();

        Uri uri = getIntent().getData();
        if (uri != null) {
            openFragment(uri.toString());
        } else {
            String url = getIntent().getStringExtra(KEY_URL);
            if (url != null) {
                openFragment(url);
            } else {
                SnackbarUtils.Short(findViewById(R.id.container_frame_layout), "数据出错！").show();
                finish();
            }
        }
    }


    private AgentWebFragment mAgentWebFragment;

    private void openFragment(String url) {
        FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
        ft.add(R.id.container_frame_layout, mAgentWebFragment = AgentWebFragment.getInstance(url));
        ft.commit();

    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        //一定要保证 mAentWebFragemnt 回调
//		mAgentWebFragment.onActivityResult(requestCode, resultCode, data);
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        AgentWebFragment agentWebFragment = mAgentWebFragment;
        if (agentWebFragment != null) {
            FragmentKeyDown fragmentKeyDown = agentWebFragment;
            if (fragmentKeyDown.onFragmentKeyDown(keyCode, event)) {
                return true;
            } else {
                return super.onKeyDown(keyCode, event);
            }
        }
        return super.onKeyDown(keyCode, event);
    }


    @Override
    protected void onDestroy() {
        super.onDestroy();

    }


}
